/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmItemBrower.java
 *
 * Created on May 25, 2009, 11:17:40 PM
 */
package inventorymanagement;

import DataAccess.DBUtil;
import DataAccess.TableAccess;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Home
 */
public class frmItemBrower extends javax.swing.JFrame {

    static frmProcedureBill code;

    /** Creates new form frmItemBrower */
    public frmItemBrower() {
        initComponents();
        try {
            TableAccess.readToTable("select ItemID, ItemName, QuantityAvailable, Price, Status from Items", tblItems);
            TableAccess.readToCombo("select * from Categories", cboCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtItemName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtItemID = new javax.swing.JTextField();
        btnRefresh = new javax.swing.JButton();
        cboCategory = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblItems = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
            }
        };
        tblItems.getTableHeader().setReorderingAllowed(false);
        jLabel4 = new javax.swing.JLabel();
        btnSelect = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Items");
        setResizable(false);

        txtItemName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemNameKeyReleased(evt);
            }
        });

        jLabel3.setText("Item Name:");

        jLabel2.setText("Item ID:");

        txtItemID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtItemIDKeyReleased(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        cboCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All" }));
        cboCategory.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCategoryItemStateChanged(evt);
            }
        });

        tblItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblItems.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblItemsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblItems);

        jLabel4.setText("Category");

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel2)
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, txtItemName)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, txtItemID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 132, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(jLabel3))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel4)
                                .add(18, 18, 18)
                                .add(cboCategory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(btnRefresh)))
                    .add(btnSelect))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtItemID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)
                    .add(cboCategory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(txtItemName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnRefresh))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 171, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btnSelect)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtItemNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemNameKeyReleased
        String str = txtItemName.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select ItemID, ItemName, QuantityAvailable, Price, Status from Items";
        } else {
            sql = "select ItemID, ItemName, QuantityAvailable, Price, Status from Items where ItemName like '%" + str + "%'";
        }
        try {
            TableAccess.readToTable(sql, tblItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_txtItemNameKeyReleased

    private void txtItemIDKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtItemIDKeyReleased
        String str = txtItemID.getText();
        String sql = "";
        if (str.equals("")) {
            sql = "select ItemID, ItemName, QuantityAvailable, Price, Status from Items";
        } else {
            sql = "select ItemID, ItemName, QuantityAvailable, Price, Status from Items where ItemID like '%" + str + "%'";
        }
        try {
            TableAccess.readToTable(sql, tblItems);
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_txtItemIDKeyReleased

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        refresh();
}//GEN-LAST:event_btnRefreshActionPerformed

    private void cboCategoryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCategoryItemStateChanged
        String cateName = cboCategory.getSelectedItem().toString();
        int cateid = 0;
        if (cateName.equals("All")) {
            try {
                DataAccess.TableAccess.readToTable("select * from Items", tblItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                String sql = "select CategoryID from Categories where CategoryName='" + cateName + "'";
                Statement st = DBUtil.getConnection(DBUtil.getInfoDB()).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                //ResultSet rs = DataInteraction.queryResultSet("select CategoryID from Categories where CategoryName='" + cateName+"'");
                ResultSet rs = st.executeQuery(sql);
                while (rs.next()) {
                    cateid = rs.getInt("CategoryID");
                }
            } catch (SQLException ex) {
                Logger.getLogger(frmSearchItems.class.getName()).log(Level.SEVERE, null, ex);
            }
            String sql = "select * from Items where CategoryID=" + cateid;
            try {
                TableAccess.readToTable(sql, tblItems);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}//GEN-LAST:event_cboCategoryItemStateChanged

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int itemID = tblItems.getSelectedRow();
        if (itemID == -1) {
            JOptionPane.showMessageDialog(this, "You must choose a item");
            return;
        } else {
            int row = tblItems.getSelectedRow();
            code.txtItemID.setText(tblItems.getValueAt(row, 0).toString());
            this.dispose();
        }
}//GEN-LAST:event_btnSelectActionPerformed

    private void tblItemsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblItemsMouseClicked
        // TODO add your handling code here:
        int row = tblItems.getSelectedRow();
        String str = tblItems.getValueAt(row, 0).toString();
        String str1 = tblItems.getValueAt(row, 1).toString();
        txtItemID.setText(str);
        txtItemName.setText(str1);
    }//GEN-LAST:event_tblItemsMouseClicked
    private void refresh() {
        txtItemID.setText("");
        txtItemName.setText("");
        try {
            TableAccess.readToTable("select ItemID, ItemName, QuantityAvailable, Price, Status from Items", tblItems);
            //TableAccess.readToCombo("select * from Categories", cboCategory, "CategoryName");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new frmItemBrower().setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSelect;
    private javax.swing.JComboBox cboCategory;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblItems;
    private javax.swing.JTextField txtItemID;
    private javax.swing.JTextField txtItemName;
    // End of variables declaration//GEN-END:variables
}
